package org.dreamwork.jasmine2.jxd;

import org.apache.log4j.Logger;
import org.dreamwork.util.Collection;
import org.dreamwork.util.ICollection;
import org.dreamwork.jasmine2.web.controls.Page;
import org.dreamwork.jasmine2.engine.HttpContext;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2008-12-24
 * Time: 14:29:31
 */
public class ClientScriptManager {
//    private Map clientScriptBlocks = new HashMap ();
    private Collection<String> clientScriptBlocks;// = new Collection ();
    private Collection<String> startupStatements;// = new Collection ();
    private Collection<String> initStatements;// = new Collection ();
    private Collection<String> style;// = new Collection ();

    private Page page;

    private static final Logger logger = Logger.getLogger (ClientScriptManager.class);

    @SuppressWarnings ("unchecked")
    public ClientScriptManager (Page page) {
        this.page = page;
        HttpContext context = HttpContext.current ();
        clientScriptBlocks = (Collection<String>) context.getAttribute ("clientScriptBlocks");
        if (clientScriptBlocks == null) {
            clientScriptBlocks = new Collection<String> ();
            context.setAttribute ("clientScriptBlocks", clientScriptBlocks);
        }
        startupStatements = (Collection<String>) context.getAttribute ("startupStatements");
        if (startupStatements == null) {
            startupStatements = new Collection<String> ();
            context.setAttribute ("startupStatements", startupStatements);
        }
        initStatements = (Collection<String>) context.getAttribute ("initStatements");
        if (initStatements == null) {
            initStatements = new Collection<String> ();
            context.setAttribute ("initStatements", initStatements);
        }
        style = (Collection<String>) context.getAttribute ("style");
        if (style == null) {
            style = new Collection<String> ();
            context.setAttribute ("style", style);
        }
    }

    public void registerClientScriptBlock (Class clazz, String script) {
        if (clientScriptBlocks.containsKey (script)) return;
        try {
            String resource = InternalResourceManager.getResource (clazz, script);
            String block = "<script language=\"javascript\" type=\"text/javascript\" ";

            block += "src=\"" + page.getHttpContext ().getRequest ().getContextPath ();
            block += "/InternalResource.jxd?" + resource + "\"></script>\n";
            clientScriptBlocks.add (script, block);
        } catch (Exception ex) {
            logger.error (ex.getMessage (), ex);
        }
    }

    public void registerStyleFile (Class clazz, String style) {
        if (this.style.containsKey (style)) return;
        try {
            String resource = InternalResourceManager.getResource (clazz, style);
            String block = "<link rel=\"StyleSheet\" type=\"text/css\" href=\"" +
                           page.getHttpContext ().getRequest ().getContextPath ();
            block += "/InternalResource.jxd?" + resource + "\"/>";
            this.style.add (style, block);
        } catch (Exception ex) {
            logger.error (ex.getMessage (), ex);
        }
    }

    public void addInitStatement (String name, String statement) {
        if (!initStatements.containsKey (name))
            initStatements.add (name, statement);
    }

    public void addStartupStatement (String statement) {
        startupStatements.add (statement, statement);
    }

    public ICollection getInitStatements () {
        return initStatements;
    }

    public ICollection getClientScriptBlocks () {
        return clientScriptBlocks;
    }

    public ICollection getStateupStatements () {
        return startupStatements;
    }

    public ICollection getStyles () {
        return style;
    }

    public void clear () {
        clientScriptBlocks.clear ();
        startupStatements.clear ();
        initStatements.clear ();
        style.clear ();
    }
}